package com.action.admin.user;

import java.util.List;

import com.action.BaseAction;
import com.dao.FunctionDao;
import com.dao.LoginDao;
import com.entity.Employee;
import com.entity.Function;
import com.util.AppUtil;

import org.apache.commons.lang.xwork.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.ResultPath;

/**
 * 
 * @author Administrator
 * 
 */
 @ParentPackage("json-default")
//@ParentPackage("extendsjson-default")
@Namespace("/user")
@ResultPath("/WEB-INF/pages")
public class LoginAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Action(value = "login", results = { @Result(name = "success", location = "../main.jsp"),
			@Result(name = "error", location = "/error.jsp") })
	public String login() {
		try {
			// 取得session保存中的字符进行对比
			String arandom = (String) (this.session("random"));// username,
			// AppUtil.MD5(this.password)
			String randcode = rand.toUpperCase();
			if (!StringUtils.isNotBlank(username) || !StringUtils.isNotBlank(password)) {
				status = "0";// 用户名或密码为空
				return ERROR;
			} else if (!arandom.equals(randcode)) {
				status = "1";// 验证码不正确
				return ERROR;
			} else {
				Employee emp = loginDao.findEmpByNamePwd(username, AppUtil.MD5(password));
				if (emp == null) {
					status = "2";// 用户不存在或用户名密码不正确
					return ERROR;
				} else {
					funclist = functionDao.findFunctionsByRole(emp.getRoleid());
					this.setSession("funclist", funclist);
					this.setSession("userinfo", emp);
					return SUCCESS;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			status = "9";// 系统异常
			return ERROR;
		}
	}

	/**
	 * 登录
	 * @return
	 */
	@Action(value = "index", results = { @Result(name = "success", location = "index.jsp") })
	public String index() {
		// flag==1为session超时
		if (flag == 1) {
			status = "3";
		}
		return SUCCESS;
	}

	/**
	 * 登出
	 * @return
	 */
	@Action(value = "logout", results = { @Result(name = "success", location = "index.do?status=4", type = "redirect") })
	public String logout() {
		this.removeSession("funclist");
		this.removeSession("userinfo");
		return SUCCESS;
	}
	
	// @com.base.BeanFactory(DIClass = LoginService.class)
	private LoginDao loginDao = (LoginDao) ioc.newInstance(LoginDao.class);
	private FunctionDao functionDao = (FunctionDao) ioc.newInstance(FunctionDao.class);
	private String username;
	private String password;
	private String rand;
	private String status = "-1";
	private Function function;
	private int flag = 0;// session超时状态

	public int getFlag() {
		return flag;
	}

	public void setFlag(int flag) {
		this.flag = flag;
	}

	private List funclist;

	public List getFunclist() {
		return funclist;
	}

	public void setFunclist(List funclist) {
		this.funclist = funclist;
	}

	public Function getFunction() {
		return function;
	}

	public void setFunction(Function function) {
		this.function = function;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getRand() {
		return rand;
	}

	public void setRand(String rand) {
		this.rand = rand;
	}
}
